package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

/**
 * @author xiaycbstart
 * @create 2019 12 19 10:58
 */
public interface CheckGroupDao {
    //新增
    @Insert("insert into t_checkgroup values(null,#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @Options(useGeneratedKeys = true ,keyColumn = "id",keyProperty = "id")
    void addCheckGroup(CheckGroup checkGroup);
    //新增检查组与检查项关系
    @Insert("insert into t_checkgroup_checkitem values(#{checkgroup_id},#{checkitem_id})")
    void addRelationGroupAndItem(Map map);
    //查询
    @Select("<script>" +
            "select * from t_checkGroup\n" +
            "<if test=\"value != null and value.length > 0\">\n" +
            "where code like '%' #{value} '%' or name like '%' #{value} '%' or helpCode like '%' #{value} '%'\n" +
            "</if>" +
            "</script>")
    Page<CheckGroup> pageQuery(String value);
    //删除关系
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id = #{id}")
    void deleteRelationGroupAndItem(Integer id);
    //删除检查组
    @Delete("delete  from t_checkgroup where id = #{id}")
    void deleteById(Integer id);
    //根据检查组id查检查项ids
    @Select("select checkitem_id from t_checkgroup_checkitem where checkgroup_id = #{id}")
    List<Integer> findCheckItemIdsByCheckGroupId(Integer id);
    //编辑更新检查组
    @Update("<script>" +
                "update t_checkgroup\n" +
                    "<set>\n" +
                        "<if test=\"name != null\">\n" +
                            "name = #{name},\n" +
                        "</if>\n" +
                        "<if test=\"sex != null\">\n" +
                            "sex = #{sex},\n" +
                        "</if>\n" +
                        "<if test=\"code != null\">\n" +
                            "code = #{code},\n" +
                        "</if>\n" +
                        "<if test=\"helpCode != null\">\n" +
                            "helpCode = #{helpCode},\n" +
                        "</if>\n" +
                        "<if test=\"attention != null\">\n" +
                            "attention = #{attention},\n" +
                        "</if>\n" +
                        "<if test=\"remark != null\">\n" +
                            "remark = #{remark},\n" +
                        "</if>\n" +
                    "</set>\n" +
                "where id = #{id}" +
            "</script>")
    void update(CheckGroup checkGroup);
    @Select("select * from t_checkgroup")
    List<CheckGroup> findAll();
    @Select("select * from t_checkgroup t1,t_setmeal_checkgroup t2 where t2.setmeal_id = #{id} and t2.checkgroup_id = t1.id")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "code",column = "code"),
            @Result(property = "helpCode",column = "helpCode"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "age",column = "age"),
            @Result(property = "remark",column = "remark"),
            @Result(property = "attention",column = "attention"),
            @Result(property = "checkItems",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckItemDao.findByCheckgroupId"))
    })
    List<CheckGroup> findBySetmealId(Integer id);
}
